package com.example.qu_tao.View;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.qu_tao.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShouCangActivity extends AppCompatActivity implements View.OnClickListener {
    private GridView gridView;
    private LinearLayout emptyLayout;
    private List<Map<String, Object>> dataList;
    private SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoucang);

        initView();
        loadCollectionData();
    }

    private void initView() {
        // 返回按钮
        ImageView backBtn = findViewById(R.id.back_btn);
        backBtn.setOnClickListener(this);

        // 标题
        TextView titleView = findViewById(R.id.title);
        titleView.setText("我的收藏");

        gridView = findViewById(R.id.grid_view);
        emptyLayout = findViewById(R.id.empty_layout);

        dataList = new ArrayList<>();
        adapter = new SimpleAdapter(this, dataList, R.layout.item_collection,
                new String[]{"goods_name", "goods_price"},
                new int[]{R.id.tv_goods_name, R.id.tv_goods_price});
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 点击收藏商品跳转到商品详情页
                int goodsId = (int) dataList.get(position).get("goods_id");
                Intent intent = new Intent(ShouCangActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goodsId", goodsId);
                startActivity(intent);
            }
        });
    }

    @SuppressLint("Range")
    private void loadCollectionData() {
        Cursor cursor = getContentResolver().query(Uri.parse("content://com.example.qu_tao.provider/collection"), null, null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            emptyLayout.setVisibility(View.GONE);
            gridView.setVisibility(View.VISIBLE);

            dataList.clear();
            while (cursor.moveToNext()) {
                Map<String, Object> map = new HashMap<>();
                map.put("goods_id", cursor.getInt(cursor.getColumnIndex("goods_id")));
                map.put("goods_name", cursor.getString(cursor.getColumnIndex("goods_name")));
                map.put("goods_price", "¥" + cursor.getDouble(cursor.getColumnIndex("goods_price")));
                dataList.add(map);
            }
            adapter.notifyDataSetChanged();
            cursor.close();
        } else {
            emptyLayout.setVisibility(View.VISIBLE);
            gridView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_btn) {
            finish(); // 返回上一页
        }
    }
    public void goShopping(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish(); // 关闭当前收藏页面
    }
}